import axios from "axios";
import Cookie from "js-cookie";
import { message } from "antd";

const request = axios.create({
  timeout: 3000,
  timeoutErrorMessage: "请求超时",
});

request.interceptors.request.use(
  (config) => {
    if (Cookie.get("token")) {
      config.headers.authorization = Cookie.get("token");
    }

    return config;
  },
  (err) => {
    return Promise.reject(err);
  }
);

request.interceptors.response.use(
  (config) => config.data,
  (err) => {
    message.error(err.response.data.msg);
    
    const status = err.response.status;
    switch (status) {
      case 402:
        Cookie.remove("token");
        window.location.href = "/login";
      default:
        return Promise.reject(err);
    }
  }
);

export default request;
